Using BitSet in Java 在Java中使用BitSet

Categories: Java; Tagged with: ; @ July 16th, 2012 21:50

This class implements a vector of bits that grows as needed. Each component of the bit set has a boolean value. The bits of a BitSet are indexed by nonnegative integers. Individual indexed bits can be examined, set, or cleared. One BitSet may be used to modify the contents of another BitSet through logical AND, logical inclusive OR, and logical exclusive OR operations.
By default, all bits in the set initially have the value false, for example:

BitSet bs = new BitSet(9); // Create a new bit set,the initial size is 9;
	bs.set(3); // set the value of index 3 to true;
	// bs.clear(3); // clear - set to false;
	bs.set(10);

	// the number of bits currently in this bit set, the last true index + 1;
	System.out.println(bs.size());
	// the bit length.
	System.out.println(bs.length());
	// the true index.
	System.out.println(bs.toString());

	// set to the complement of its current.
	bs.flip(3);

	System.out.println(bs.toString());

	// create a new bitset.
	BitSet bs3 = new BitSet();
	bs3.set(10); 
	System.out.println(bs.equals(bs3)); // true

	bs.clear(); // clear all
	bs.xor(bs3); // XOR
	System.out.println(bs);
}

<->



// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.